package com.yazi.mxz.mobile.controller;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.hibernate.Page;
import com.myframework.smhj.hibernate.Paging;
import com.myframework.smhj.hibernate.Searching;
import com.myframework.smhj.hibernate.SearchingFactory;
import com.myframework.smhj.jackson.result.PageResult;
import com.yazi.mxz.common.entity.beans.Ad;
import com.yazi.mxz.common.entity.beans.Store;
import com.yazi.mxz.common.entity.beans.StoreGoods;
import com.yazi.mxz.common.entity.enums.AdDevType;
import com.yazi.mxz.common.entity.enums.StoreGoodsStatus;
import com.yazi.mxz.common.service.AdService;
import com.yazi.mxz.common.service.StoreGoodsService;
import com.yazi.mxz.common.service.StoreService;

@Controller("storeContoller")
@RequestMapping("/store")
public class StoreController extends BaseController {
	private static Logger log = LoggerFactory.getLogger(StoreController.class);
	
	@Resource
	StoreService storeService;
	@Resource
	AdService adService;
	@Resource
	StoreGoodsService storeGoodsService;

	@RequestMapping("/{storeId}")
	public String indexForShop(@PathVariable long storeId, Model model){
		log.debug("进入店铺主页");
		Store store = storeService.one(storeId);
		if (null == store) {
			return REDIRECT_INDEX;
		}
		
		/* 焦点广告 */
		Ad ad4Search = new Ad();
		ad4Search.setStoreId(storeId);
		ad4Search.setStatus(true);
		ad4Search.setDevType(AdDevType.TOUCH.getValue()); // 手机触屏版
		Paging piForFocus = new Searching();
		piForFocus.setPageSize(5);
		List<Ad> adList = adService.list(ad4Search, piForFocus).getList();
		
		/* 今日推荐 start */
		/* 今日特价 */
		StoreGoods sg4Today = new StoreGoods();
		sg4Today.setStore(store);
		sg4Today.setStatus(StoreGoodsStatus.ON.ordinal());
		sg4Today.setAtDiscount(true);// 打折
		
		Paging pi4Today= new Searching();
		pi4Today.setPageSize(4);
	
		List<StoreGoods> todayList = storeGoodsService.list(sg4Today, pi4Today).getList();
		
		// 没有特价商品,则取按排序取
		if (todayList.size() == 0) {
			sg4Today.setAtDiscount(false);
			Searching si4Today = SearchingFactory.createSearching("sort", 4);
			todayList = storeGoodsService.list(sg4Today, si4Today).getList();
		}
		/* 今日推荐 end */
		
		
		/* 热门商品 start */
		StoreGoods sg4Hot = new StoreGoods();
		sg4Hot.setStore(store);
		sg4Hot.setStatus(StoreGoodsStatus.ON.ordinal());
		
		// 按月销量倒序
		Searching si4Hot = SearchingFactory.createSearching("dealMonth", true, 4);
		
		List<StoreGoods> hotList = storeGoodsService.list(sg4Hot, si4Hot).getList();
		/* 热门商品 end */
		
		model.addAttribute("store", store);
		model.addAttribute("adList", adList);
		model.addAttribute("todayList", todayList);
		model.addAttribute("hotList", hotList);
		return "store_index";
	}
	
	
	@RequestMapping("/list")
	public String list(String searchWord, Model model) {
		log.debug("进入店铺列表页面");
		model.addAttribute("searchWord", searchWord);
		return "store_list";
	}
	
	@RequestMapping("/list/json")
	@ResponseBody
	public PageResult<?> listJson(Store store, Searching si) {
		log.debug("获取店铺列表");
		PageResult<Store> jsonResult = new PageResult<Store>();
		try {
			store.setIsOpen(true);
			Page<Store> page = storeService.list(store, si);
			jsonResult.setResult(page);
			jsonResult.setOk();
		} catch (Exception e) {
			log.error("获取店铺列表时异常：", e);
			jsonResult.setErrorMessage("获取店铺列表失败!");
		}
		return jsonResult;
	}
}
